package com.cqcet.wenchuang.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
<<<<<<< HEAD
import com.cqcet.wenchuang.common.R;
import com.cqcet.wenchuang.entity.Admin;
import com.cqcet.wenchuang.entity.LoginAdmin;
import com.cqcet.wenchuang.mapper.AdminMapper;
import com.cqcet.wenchuang.service.AdminService;
import com.cqcet.wenchuang.util.JwtUtil;
import com.cqcet.wenchuang.util.RedisCache;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Service;

import java.util.Objects;

@Service
public class AdminServiceImpl extends ServiceImpl<AdminMapper, Admin> implements AdminService {

    @Autowired
    private PasswordEncoder passwordEncoder;

    @Autowired
    private AuthenticationManager authenticationManager;

    @Autowired
    private RedisCache redisCache;

    /**
     * 商家注册
     *
     * @param admin
     */
    @Override
    public void register(Admin admin) {
        String password = passwordEncoder.encode(admin.getPassword());
        admin.setPassword(password);
        admin.setType(1);
        this.save(admin);
    }

    /**
     * 管理员||管理员登录
     *
     * @param admin
     * @return
     */
    @Override
    public R<Admin> login(Admin admin) {
        UsernamePasswordAuthenticationToken authenticationToken = new UsernamePasswordAuthenticationToken(admin.getUsername(), admin.getPassword());
        Authentication authenticate = authenticationManager.authenticate(authenticationToken);
        if (Objects.isNull(authenticate)) {
            throw new RuntimeException("用户名或密码错误");
        }
        //使用userId生成token
        LoginAdmin loginAdmin = (LoginAdmin) authenticate.getPrincipal();
        String userId = loginAdmin.getAdmin().getId().toString();
        Admin adminInfo = loginAdmin.getAdmin();
        adminInfo.setPassword(null);
        String jwt = JwtUtil.createJWT(userId);
        //authenticate存入redis
        redisCache.setCacheObject("login:" + userId, loginAdmin);
        //把token响应给前端
        return R.success(adminInfo).add("token", jwt);
    }

    /**
     * 管理员||管理员登出
     *
     * @return
     */
    @Override
    public R<String> logout() {
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        LoginAdmin loginAdmin = (LoginAdmin) authentication.getPrincipal();
        Long adminId = loginAdmin.getAdmin().getId();
        redisCache.deleteObject("login:" + adminId);
        return R.success("退出成功");
    }
=======
import com.cqcet.wenchuang.entity.Admin;
import com.cqcet.wenchuang.mapper.AdminMapper;
import com.cqcet.wenchuang.service.AdminService;
import org.springframework.stereotype.Service;

@Service
public class AdminServiceImpl extends ServiceImpl<AdminMapper, Admin> implements AdminService {
>>>>>>> ae245ae (数据表和实体类的修改)
}
